package thread_1;

import java.util.concurrent.*;

/**
 * @Author Klys
 * @Date 2023/3/5 13:27
 * @Description
 * @Version 1.0
 */
public class ThreadPool2 {
    public static void main(String[] args) {
// 自定义线程池！工作 ThreadPoolExecutor
        ExecutorService threadPool = new ThreadPoolExecutor(
                2,
                5,
                3,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        try {
// 最大承载：Deque + max
// 超过 RejectedExecutionException
            for (int i = 1; i <= 9; i++) {
                threadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " ok");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdown();
        }
    }
}
